package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.business.BusinessType;
import com.heima.pojo.node.*;
import com.heima.pojo.region.Region;
import com.heima.pojo.region.RegionVO;
import com.heima.service.NodeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class NodeServiceImpl implements NodeService {
    private final NodeMapper nodeMapper;
    private final RegionMapper regionMapper;
    /**
     * 点位搜索
     * @param param
     * @return
     */
    @Override
    public NodePageResult search(NodeQueryParam param) {
        log.info("点位搜索：{}",param);
        PageHelper.startPage(param.getPageIndex(), param.getPageSize());
        Page<NodeVO> page = nodeMapper.search(param);
        List<NodeVO> pageResult = page.getResult();
        if (!CollectionUtils.isEmpty(pageResult)){
            for (NodeVO nodeVO : pageResult) {
                String regionId = nodeVO.getRegionId();
                RegionVO regionVO = regionMapper.getRegionById(regionId);
                nodeVO.setRegion(regionVO);
                Integer businessId = nodeVO.getBusinessId();
                BusinessType businessType = nodeMapper.getBusinessTypeById(businessId);
                nodeVO.setBusinessType(businessType);
            }
        }

        String pageIndex = String.valueOf(param.getPageIndex());
        String pageSize = String.valueOf(param.getPageSize());
        String pages = String.valueOf(page.getPages());
        String total = String.valueOf(page.getTotal());
        NodePageResult result = new NodePageResult(pageIndex,pageSize, pages, total, page.getResult());
        return result;
    }

    /**
     * 查询商圈列表
     * @return
     */
    @Override
    public List<BusinessType> getBusinessType() {
       return nodeMapper.getBusinessType();
    }

    /**
     * 新增点位
     * @param node
     */
    @Override
    public void addNode(Node node) {
        log.info("新增点位：{}",node);
        node.setCreateTime(LocalDateTime.now());
        node.setUpdateTime(LocalDateTime.now());
        nodeMapper.addNode(node);
    }

    /**
     * 点位详情
     * @param id
     * @return
     */
    @Override
    public List<VendingMachine> getVmList(String id) {
        return nodeMapper.getVmList(id);
    }

    /**
     * 修改点位
     * @param node
     */
    @Override
    public void updateNode(Node node) {
        log.info("修改点位：{}",node);
        nodeMapper.updateNode(node);
    }

    /**
     * 删除点位
     * @param nodeId
     */
    @Override
    public void deleteNode(String nodeId) {
        log.info("删除点位：{}", nodeId);
        nodeMapper.deleteNode(nodeId);
    }
}
